package com.bookmanager.www.service.impl;

import cn.hutool.core.util.IdUtil;
import com.bookmanager.www.common.utils.StringUtils;
import com.bookmanager.www.entity.FlBookOrder;
import com.bookmanager.www.entity.FlBookTemp;
import com.bookmanager.www.mapper.FlBookorderMapper;
import com.bookmanager.www.service.FlBookorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 图书订单 服务实现类
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@Service
public class FlBookorderServiceImpl extends ServiceImpl<FlBookorderMapper, FlBookOrder> implements FlBookorderService {


    @Resource
    FlBookorderMapper flBookorderMapper;

    @Override
    public FlBookOrder createOrderPlus(String userId, double cartPrice, String addressId,Integer paymentType) {
        //生成唯一订单Id
        String orderId = StringUtils.getOutTradeNo();

        //订单状态 未付款
        Integer status = 0;

        //默认6块钱运费
        Integer freight = 6;
        String flag="余额充值";

//        //默认支付类型 支付宝支付
//        Integer paymentType = 1;


        if(flag.equals(addressId))
        {
            freight=0;
//            paymentType=3;
        }

        //实际付款
        double payment = cartPrice+freight;

        BigDecimal pay =new BigDecimal(payment);

        //创建订单时间
        LocalDateTime createdDate=LocalDateTime.now();
        //修改订单时间
        LocalDateTime updateDate=LocalDateTime.now();


        Integer insert = flBookorderMapper.createOrder(orderId,userId,status,pay,paymentType,freight,addressId,createdDate,updateDate);

        if (insert!=null)
        {
            FlBookOrder flBookOrder = flBookorderMapper.selectOrderByOrderId(orderId);
            return flBookOrder;
        }

        return null;

    }

    @Override
    public FlBookOrder createOrder(String userId, double cartPrice, String addressId) {

        //生成唯一订单Id
        String orderId = StringUtils.getOutTradeNo();

        //订单状态 未付款
        Integer status = 0;

        //默认6块钱运费
        Integer freight = 6;
        String flag="余额充值";

        //默认支付类型 支付宝支付
        Integer paymentType = 1;


        if(flag.equals(addressId))
        {
            freight=0;
            paymentType=3;
        }

        //实际付款
        double payment = cartPrice+freight;


        BigDecimal pay =new BigDecimal(payment);



        //创建订单时间
        LocalDateTime createdDate=LocalDateTime.now();
        //修改订单时间
        LocalDateTime updateDate=LocalDateTime.now();


      Integer insert = flBookorderMapper.createOrder(orderId,userId,status,pay,paymentType,freight,addressId,createdDate,updateDate);

      if (insert!=null)
      {
          FlBookOrder flBookOrder = flBookorderMapper.selectOrderByOrderId(orderId);
          return flBookOrder;
      }

        return null;
    }

    @Override
    public FlBookTemp createOrderTemp(String orderId, String bookId, Integer count) {

        //临时tempId
        String tempId= IdUtil.getSnowflake().nextIdStr();

        FlBookTemp flBookTemp=new FlBookTemp(tempId,orderId,bookId,count);

        int insert = flBookorderMapper.createOrderTemp(tempId,orderId,bookId,count);

        System.out.println("插入了几个临时订单项？"+insert);

        FlBookTemp flBookTemp1 = flBookorderMapper.selectOrderTempByTempId(tempId);

        return flBookTemp;
    }
}
